package com.hezuti.common.utils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;


/**
 * 提供一个读取不同resource文件的类,可以更改一下,明白的实现多例
 */
public class PropertyManager {
	private static final String CONFIG = "config";

	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(CONFIG);

	private static Properties properties = null;

	/**
	 * 读取默认的config.properties文件
	 * 
	 * @param key
	 * @return value
	 */
	public static String getString(String key) {
		try {
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			throw new RuntimeException("! config : " + key + '!');
		}
	}

	/**
	 * 读取指定为rb.properties文件
	 * 
	 * @param rb
	 *            property 文件，文件名为 rb.properties
	 * @param key
	 * @return value
	 */
	public static String getRB(String rb, String key) throws Exception {
		try {
			return ResourceBundle.getBundle(rb).getString(key);
		} catch (MissingResourceException e) {
			throw new RuntimeException('!' + rb + ":" + key + '!');
		}
	}
	/**
	 * 
	 * @param baseName
	 * @return
	 */
	public static Map<String, String> getStringMap(String baseName) {
		Map<String, String> m = new HashMap<String, String>();
		ResourceBundle rb = ResourceBundle.getBundle(baseName);
		Enumeration<String> keys = rb.getKeys();
		String temp;
		while (keys.hasMoreElements()) {
			temp = keys.nextElement();
			m.put(temp, rb.getString(temp));
		}
		ResourceBundle.clearCache();
		return m;
	}
}
